import requests


class EmpManageApi(object):
    def add_emp(self, req_header, req_body):  # 添加员工
        resp = requests.post(url="http://ihrm2-test.itheima.net/api/sys/user",
                             headers=req_header,
                             json=req_body)
        return resp

    def query_emp(self, req_header, emp_id):  # 查询员工
        resp = requests.get(url="http://ihrm2-test.itheima.net/api/sys/user/" + emp_id,
                            headers= req_header)
        return resp

    def modify_emp(self, emp_id, req_header, req_body):  # 修改员工
        resp = requests.put(url="http://ihrm2-test.itheima.net/api/sys/user/" + emp_id,
                            headers=req_header,
                            json=req_body)
        return resp

    def del_emp(self, emp_id, req_header):  # 删除员工
        resp = requests.delete(url="http://ihrm2-test.itheima.net/api/sys/user/" + emp_id,
                               headers=req_header)
        return resp


if __name__ == '__main__':
    # 创建实例
    emp = EmpManageApi()
    # 获取令牌
    resp = requests.post(url="http://ihrm2-test.itheima.net/api/sys/login",
                         json={"mobile": "13800000002", "password": "123456"})
    token = resp.json().get("data")
    # 准备请求头
    req_header = {"Content-Type": "application/json", "Authorization": token}
    # 准备请求体
    req_body = {"username": "张好", "mobile": "13546789034", "workNumber": "013"}
    # 准备员工id
    emp_id = "1075383135459430400"
    # 准备修改员工的请求体
    emp_mod_req_body={"username": "张大"}

    # 调用添加员工
    resp = emp.add_emp(req_header, req_body)
    print("添加员工:", resp.json())
    # 调用查询员工
    resp = emp.query_emp(req_header, emp_id)
    print("查询员工:", resp.json())
    # 调用修改员工
    resp = emp.modify_emp(emp_id,req_header,emp_mod_req_body)
    print("修改员工:", resp.json())

    # 调用删除员工
    resp = emp.del_emp(emp_id,req_header)
    print("删除员工:", resp.json())
